package com.extrace.server.controller;

import com.extrace.server.config.AjaxResult;
import com.extrace.server.dto.TransnodeDTO;
import com.extrace.server.dto.TranspackageDTO;
import com.extrace.server.mapper.TransnodeMapper;
import com.extrace.server.mapstruct.TransnodeConvert;
import com.extrace.server.mapstruct.TranspackageConvert;
import com.extrace.server.pojo.CodeNamePair;
import com.extrace.server.pojo.Transnode;
import com.extrace.server.service.TransnodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
@CrossOrigin
@RestController
public class TransnodeController {
    @Autowired
    TransnodeService transnodeService;

    @Resource
    TransnodeMapper transnodeMapper;

    @PostMapping("/Misc/Transnode/getTransnodeListByRegion")
    public AjaxResult getTransnodeListByRegion(@RequestBody Map<String, Object> params) {
        String regionCode = (String) params.get("regionCode");
        regionCode = regionCode.substring(0, 4);
        List<Transnode> nodes = transnodeMapper.findListByRegionCode(regionCode);
        List<TransnodeDTO> dtos = TransnodeConvert.INSTANCE.pos2dtos(nodes);
        for(TransnodeDTO dto: dtos) {
            dto.setProvinceCode(dto.getRegionCode().substring(0, 2)+"0000");
            dto.setCityCode(dto.getRegionCode().substring(0, 4)+"00");
            dto.setDistrictCode(dto.getRegionCode());
        }
        return AjaxResult.success(dtos);
    }

    @GetMapping("/Misc/Transnode/getAllTransnodeList")
    public List<CodeNamePair> getAllTransnodeList(HttpServletResponse response) {
        response.addHeader("state", "read_success");
        return transnodeService.findAllTrans();
    }

    @GetMapping("/Misc/Transnode/getTransnodeListByRegionCode/{regionCode}")
    public List<CodeNamePair> getTransnodeListByRegionCode(@PathVariable int regionCode, HttpServletResponse response) {
        response.addHeader("state", "read_success");
        return transnodeService.findByRegionCodeTrans(regionCode);
    }
    @GetMapping("/Misc/Transnode/getTransnodeById/{id}")
    public Transnode getTransnodeListById(@PathVariable String id, HttpServletResponse response) {
        response.addHeader("state", "read_success");
        return transnodeService.findById(id);
    }
}
